#include <stdio.h>
#include "refresh_data.h"

static const char *TAG = "refresh";

static time_t now = 0;
static struct tm timeinfo = {0};
static struct tm pre_time = {0};
static char now_time[26] = {0};
weather_data_t weather_data = {0};
static char now_info[26] = {0};
static char now_date[26] = {0};
static char now_week[26] = {0};
int refr_all = 0;

void time_Refresh(void *param)
{

    while (1)
    {
        time(&now);
        localtime_r(&now, &timeinfo);
        if (refr_all)
        {
            refr_all = 0;
            Refresh_DataSwitch(REFRESH_WEATHER_MAIN_TIME);
            Refresh_DataSwitch(REFRESH_WEATHER_MAIN_DATA);
        }
        if (timeinfo.tm_min > pre_time.tm_min)
        {
            pre_time = timeinfo;
            Refresh_DataSwitch(REFRESH_WEATHER_MAIN_TIME);
        }
        else if (timeinfo.tm_hour > pre_time.tm_hour)
        {
            pre_time = timeinfo;
            Refresh_DataSwitch(REFRESH_WEATHER_MAIN_TIME);
            Refresh_DataSwitch(REFRESH_WEATHER_MAIN_DATA);
        }
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
void week_sele(char *week,int index)
{
    switch (index)
    {
    case 5/* constant-expression */:
        /* code */
        strcpy(week,"周五");
        break;
    
    default:
        break;
    }
}

void Refresh_DataSwitch(uint8_t nowSta)
{

    switch (nowSta)
    {
    case REFRESH_WEATHER_MAIN_TIME:
    {
        /* constant-expression */
        /* code */
        pre_time = timeinfo;
        if (timeinfo.tm_min < (int)10)
        {
            sprintf(now_time, "%d:#C2A970 0%d", timeinfo.tm_hour, timeinfo.tm_min);
        }
        else
        {
            sprintf(now_time, "%d:#C2A970 %d", timeinfo.tm_hour, timeinfo.tm_min);
        }
        ESP_LOGI(TAG, "%s", now_time);
        lv_msg_send(WEATHER_MAIN_TIME, now_time);
    }
    break;
    case REFRESH_WEATHER_MAIN_DATA:
    {
        lv_msg_send(WEATHER_MAIN_IMG, &weather_data.weather_code);
        // sprintf(now_info,"%s->%s",weather_data.weather_city,weather_data.weather_weat);

        lv_msg_send(WEATHER_MAIN_CITY, "湛江");
        sprintf(now_date, "%d月%d日", (timeinfo.tm_mon + 1), timeinfo.tm_mday);
        lv_msg_send(WEATHER_MAIN_DATA, now_date);
        lv_msg_send(WEATHER_MAIN_TEMP, weather_data.weather_temp);
        ESP_LOGI(TAG, "%s", now_date);
        week_sele(now_week,5);
        // lv_msg_send(WEATHER_MAIN_WEEK,now_week);
        //  lv_msg_send(WEATHER_MAIN_WEEK,"周五");
    }
    break;
    // case REFRESH_WEATHER_MAIN_DATE:
    // {
    // }
    // break;
    default:
    {
    }
    break;
    }
}
